package com.lxw.weather.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lxw.weather.R;
import com.lxw.weather.model.Observe24hData;

import java.util.List;

public class Observe24hAdapter extends RecyclerView.Adapter<Observe24hAdapter.Observe24hViewHolder> {

    private List<Observe24hData> observe24hList;
    private Context context;

    public Observe24hAdapter(List<Observe24hData> observe24hList) {
        this.observe24hList = observe24hList;
    }

    @NonNull
    @Override
    public Observe24hViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context)
                .inflate(R.layout.observe_24h_item, parent, false);

        // 修改布局参数设置，使用ViewPager2专用参数
        ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,  // 宽度填满
                ViewGroup.LayoutParams.WRAP_CONTENT   // 高度自适应
        );
        view.setLayoutParams(layoutParams);

        return new Observe24hViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Observe24hViewHolder holder, int position) {
        if (observe24hList == null || position >= observe24hList.size()) {
            return; // 防止越界
        }

        Observe24hData data = observe24hList.get(position);

        // 设置时间 - 添加null检查
        if (holder.hourlyTime != null) {
            holder.hourlyTime.setText(data.getTime()+"时");
        }

        // 设置温度 - 添加null检查
        if (holder.hourlyTemp != null) {
            holder.hourlyTemp.setText(data.getTemperature() + "℃");
        }

//        // 设置最低/最高温度范围 - 添加null检查
//        if (holder.hourlyMinMaxTemp != null) {
//            holder.hourlyMinMaxTemp.setText(data.getMinTemp() + "℃/" + data.getMaxTemp() + "℃");
//        }
//
//        // 设置天气状况 - 添加null检查
//        if (holder.hourlyWeather != null) {
//            holder.hourlyWeather.setText(data.getWeather());
//        }

        // 设置白天风向风力 - 添加null检查
        if (holder.dayWind != null) {
            holder.dayWind.setText("风向: " + data.getWindDirection() + "\\风力: " + data.getWindPower()+"级");
        }

        // 设置湿度 - 添加null检查
        if (holder.hourlyHumidity != null) {
            holder.hourlyHumidity.setText("湿度: " + data.getHumidity() + "%");
        }

        // 设置降水量 - 添加null检查
        if (holder.hourlyPrecipitation != null) {
            holder.hourlyPrecipitation.setText("降水: " + data.getPrecipitation() + "mm");
        }

        // 设置天气图标 - 添加null检查
        if (holder.hourlyIcon != null) {
            String weatherCode = data.getWeatherCode();
            int iconResId = getWeatherIconResId(weatherCode);
            if (iconResId != 0) {
                holder.hourlyIcon.setImageResource(iconResId);
            } else {
                holder.hourlyIcon.setImageResource(android.R.drawable.ic_menu_help); // 默认图标
            }
        }
    }

    @Override
    public int getItemCount() {
        return observe24hList != null ? observe24hList.size() : 0;
    }

    // 获取天气图标资源ID
    private int getWeatherIconResId(String weatherCode) {
        if (context == null || weatherCode == null) {
            return 0;
        }

        try {
            // 示例实现：根据天气代码映射到资源ID
            // 您需要确保res/drawable目录中存在对应的图标文件
            int resId = context.getResources().getIdentifier(
                    weatherCode, "mipmap", context.getPackageName());

            if (resId != 0) {
                return resId;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 默认返回未知图标
        return context.getResources().getIdentifier(
                "unknown", "mipmap", context.getPackageName());
    }

    // 添加数据更新方法
    public void setData(List<Observe24hData> dataList) {
        this.observe24hList = dataList;
        notifyDataSetChanged();
    }

    public static class Observe24hViewHolder extends RecyclerView.ViewHolder {
        TextView hourlyTime, hourlyTemp, dayWind, hourlyHumidity, hourlyPrecipitation;
        ImageView hourlyIcon;

        public Observe24hViewHolder(@NonNull View itemView) {
            super(itemView);

            hourlyTime = itemView.findViewById(R.id.hourText);
            hourlyTemp = itemView.findViewById(R.id.hourlyTemp);
            dayWind = itemView.findViewById(R.id.dayWind);
            hourlyHumidity = itemView.findViewById(R.id.hourlyHumidity);
            hourlyPrecipitation = itemView.findViewById(R.id.hourlyPrecipitation);
            //hourlyIcon = itemView.findViewById(R.id.hourlyIcon);
        }
    }
}